﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Hotel.Classes.Helper;
using Hotel.Models.Entity;

namespace Hotel.Models.DataAccess
{
    public class DACustomer
    {
        public static List<Customer> GetListCustomer(int cusID)
        {
            DataTable dt;
            List<Customer> list = new List<Customer>();
            using (DBManager dbManager = new DBManager("stp_Get_Customer"))
            {
                dbManager.Add("p_PK_CustomerID", cusID);
                dbManager.Add("IsDelete", 0);
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Customer cus = new Customer();
                cus.PkCustomerId = DataHelper.GetInt(dr, "PK_CustomerID");
                cus.Name = DataHelper.Get(dr, "Name");
                cus.Address = DataHelper.Get(dr, "Address");
                cus.Phone = DataHelper.GetInt(dr, "Phone");
                cus.Passport = DataHelper.Get(dr, "Passport");
                cus.Birthday = DataHelper.GetDate(dr, "Birthday");
                cus.BirthPlace = DataHelper.Get(dr, "BirthPlace");
                cus.NoVisa = DataHelper.Get(dr, "NoVisa");
                cus.TimeVisa = DataHelper.GetDate(dr, "TimeVisa");
                cus.Recipients = DataHelper.Get(dr, "Recipients");
                cus.EntryDate = DataHelper.GetDate(dr, "EntryDate");
                cus.SojournFrom = DataHelper.GetDate(dr, "SojournFrom");
                cus.SojournTo = DataHelper.GetDate(dr, "SojournTo");
                cus.Nationality = DataHelper.Get(dr, "Nationality");
                cus.Gender = DataHelper.GetInt(dr, "Gender");
                list.Add(cus);
            }
            return list;
        }

        public static bool DeleteCustomer(int cusID)
        {
            using (DBManager dbManager = new DBManager("stp_Get_Customer"))
            {
                try
                {
                    dbManager.Add("p_PK_CustomerID", cusID);
                    dbManager.Add("IsDelete", 1);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
                
            }
        }

        public static bool SaveCustomer(Customer cus, bool isUpdate)
        {
            using (DBManager dbManager = new DBManager("stp_Create_Customer"))
            {
                try
                {
                    dbManager.Add("p_PK_CustomerID", cus.PkCustomerId);
                    dbManager.Add("p_Name", cus.Name);
                    dbManager.Add("p_Address", cus.Address);
                    dbManager.Add("p_Phone", cus.Phone);
                    dbManager.Add("p_Passport", cus.Passport);
                    dbManager.Add("p_Birthday", cus.Birthday);
                    dbManager.Add("p_BirthPlace", cus.BirthPlace);
                    dbManager.Add("p_NoVisa", cus.NoVisa);
                    dbManager.Add("p_TimeVisa", cus.TimeVisa);
                    dbManager.Add("p_Recipients", cus.Recipients);
                    dbManager.Add("p_EntryDate", cus.EntryDate);
                    dbManager.Add("p_SojournFrom", cus.SojournFrom);
                    dbManager.Add("p_SojournTo", cus.SojournTo);
                    dbManager.Add("p_Nationality", cus.Nationality);
                    dbManager.Add("p_Gender", cus.Gender);
                    dbManager.Add("IsUpdate", isUpdate);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
                
            }
        }
    }
}